﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace KhoBanHang
{
    public partial class PhieuXKho_DanhSach : Form
    {
       
        List<Business.PhieuXuatKho> danhSachPhieuXuatKho;
        

       
        public PhieuXKho_DanhSach()
        {
            InitializeComponent();
        }


        private void PhieuXKho_DanhSach_Load(object sender, EventArgs e)
        {
            LoadDanhsachPhieuXuatKho(true);

        }

        private void LoadDanhsachPhieuXuatKho(bool lanDau)
        {
            if (lanDau)
            {

                danhSachPhieuXuatKho = Business.PhieuXuatKho.LayDanhSach();
            }
            else
            {

                if (rbtnNgayXuat.Checked && dtpickerTuNgay.Value != null)
                    danhSachPhieuXuatKho = Business.PhieuXuatKho.LayDanhSach_NgayLapPhieu(dtpickerTuNgay.Value, dtpickerDenNgay.Value);
                else if (rbtnMaHD.Checked && txtMaHD.Text.Contains("HDB"))
                    danhSachPhieuXuatKho = Business.PhieuXuatKho.LayDanhSach_MaHoaDonBan(txtMaHD.Text);
                else if (rbtnMaPhieuxuat.Checked && txtMaPhieuXuat.Text.Contains("PXK"))
                    danhSachPhieuXuatKho = Business.PhieuXuatKho.LayDanhSach_MaPhieuXuat(txtMaPhieuXuat.Text);
                else
                    danhSachPhieuXuatKho = Business.PhieuXuatKho.LayDanhSach();
            }

            gridviewDSPhieuNK.DataSource = danhSachPhieuXuatKho;
            gridviewDSPhieuNK.Columns["MaPXK"].Visible = false;

            gridviewDSPhieuNK.Columns["MaHDB"].Visible = false;
            gridviewDSPhieuNK.Columns["MaNV"].Visible = false;
            gridviewDSPhieuNK.Columns["DiaChiKhachHang"].Visible = false;
            gridviewDSPhieuNK.Columns["DienThoai"].Visible = false;
            gridviewDSPhieuNK.Columns["LyDoXuat"].Visible = false;


            gridviewDSPhieuNK.Columns["NgayXuatHD"].DefaultCellStyle.Format = "dd/MM/yyyy";

            gridviewDSPhieuNK.Columns["MaPXKHienThi"].HeaderText = "Mã phiếu xuất kho";
            gridviewDSPhieuNK.Columns["MaNVHienThi"].HeaderText = "Nhân viên lập";
            gridviewDSPhieuNK.Columns["MaHDBHienThi"].HeaderText = "Hóa đơn bán";

            gridviewDSPhieuNK.Columns["NgayXuatHD"].HeaderText = "Ngày lập phiếu";
            gridviewDSPhieuNK.Columns["TenKhachHang"].HeaderText = "Tên khách hàng";
        }
        

        
        private void btnTim_Click(object sender, EventArgs e)
        {
            LoadDanhsachPhieuXuatKho(false);
        }


        private void btnXem_Click(object sender, EventArgs e)
        {
            if (danhSachPhieuXuatKho != null && danhSachPhieuXuatKho.Count > 0)
            {
                if (gridviewDSPhieuNK.SelectedRows.Count == 0)
                {
                    int maPXK = danhSachPhieuXuatKho[0].MaPXK;

                    LoadPhieuXuatKho(maPXK);
                }
                else
                {
                    int maPXK = danhSachPhieuXuatKho[gridviewDSPhieuNK.SelectedRows[0].Index].MaPXK;
                    LoadPhieuXuatKho(maPXK);
                }
            }
 
        }

        private void LoadPhieuXuatKho(int maPXK)
        {
            PhieuXuatKho form = new PhieuXuatKho();
            form.LayTheoPXK(maPXK);
            form.ShowDialog();
        }
        
    }
}
